<?php

namespace App\Libs\Company\SheetTemplateHandler\Aggregate;

use App\Libs\Company\SheetTemplateHandler\Manager;
use App\Models\AggregateReport;

class Template202 extends AbstractTemplate
{
    public $dataStartRow = 2;

    // the aggregate template id
    protected $items = [
        '景区（点）' => 209,
        '宾馆饭店' => 210,
        '家庭旅馆' => 211,
        '项目建设' => 212,
        '旅游产品' => 213,
        '高校毕业生' => 214,
    ];

    public function generateAbstract()
    {
        $this->renderHeader();
        $this->output['sheets']['sheet-01']['cellData']['0']['0'] = $this->createTitleCell("日喀则市{$this->task->year}年{$this->task->key}月\r{$this->template->name}");

        foreach ($this->items as $name => $aggregateTemplateId) {
            $aggregateReports = AggregateReport::where([
                'sheet_template_id' => $aggregateTemplateId,
                'frequency' => $this->task->frequency,
                'year' => $this->task->year,
                'key' => $this->task->key,
            ])
                ->get();
            foreach ($aggregateReports as $aggregateReport) {
                $handler = Manager::createAggregateHandler($aggregateReport->sheetTemplate, $this->task);
                $data = $handler->getData();
                if (!$data) {
                    return '';
                }
                $column2Val = $column3Val = $column4Val = 0;
                foreach ($data as $value) {
                    $column2Val += (int) $value[2]['v'];
                    $column3Val += (int) $value[3]['v'];
                    $column4Val += (float) $value[4]['v'];
                }

                $this->output['sheets']['sheet-01']['cellData'][] = [
                    ['v' => $name, 's' => 'CENTER'],
                    ['v' => $column2Val, 't' => 2],
                    ['v' => $column3Val, 't' => 2],
                    ['v' => round($column4Val, 2), 't' => 2],
                ];
            }
        }
        $this->dataEndRow = count($this->output['sheets']['sheet-01']['cellData']) - 1;

        $this->renderFooter();
        return $this->output;
    }
}
